package com.itheima.leetcode.od.c.binary;

import java.util.Scanner;

/**
 * <h3>员工派遣</h3>
 */
public class EmployeeDispatch {
    static long x;
    static long y;
    static long cntx;
    static long cnty;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        x = sc.nextInt();
        y = sc.nextInt();
        cntx = sc.nextInt();
        cnty = sc.nextInt();

        long min = cntx + cnty;
        //    long max = Long.MAX_VALUE;  // 使用此上限，实际通过率55%
        long max = 1000000000L; // 使用此上限，实际通过率可以100%

        while (min <= max) {
            long mid = min + (max - min) / 2;

            if (check(mid)) {
                max = mid - 1;
            } else {
                min = mid + 1; // Leftmost
            }
        }

        System.out.println(min);
    }

    public static boolean check(long k) {
        long A = k / x; // 1~k范围内x倍数的数量
        long B = k / y; // 1~k范围内y倍数的数量
        long C = k / (x * y); // 1~k范围内x*y倍数的数量

        return Math.max(0, cntx - (B - C)) + Math.max(0, cnty - (A - C)) <= k - A - B + C;
    }
}